using MicroMsg.Common.Event;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Manager
{
	public class SnsMsgMgr
	{
		private const string SnsMsgUnreadCountKey = "SnsMsgUnreadCountKey";
		public static void init()
		{
			EventCenter.registerEventHandler(EventConst.ON_STORAGE_SNSMSG_ADD, new EventHandlerDelegate(SnsMsgMgr.onSnsMsgAdd));
		}
		private static void onSnsMsgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			int num = ConfigMgr.getInt("SnsMsgUnreadCountKey", 0);
			if (evtArgs != null)
			{
				List<SnsMsg> listObject = evtArgs.getListObject<SnsMsg>();
				if (listObject != null)
				{
					num += listObject.get_Count();
					ConfigMgr.setInt("SnsMsgUnreadCountKey", num);
					EventCenter.postEvent(EventConst.ON_UNREAD_SNSMSG_CNTCHANGE, null, null);
				}
			}
		}
		public static int getSnsMsgUnreadCount()
		{
			return ConfigMgr.getInt("SnsMsgUnreadCountKey", 0);
		}
		public static void setSnsMsgUnreadCount(int count)
		{
			ConfigMgr.setInt("SnsMsgUnreadCountKey", count);
			EventCenter.postEvent(EventConst.ON_UNREAD_SNSMSG_CNTCHANGE, null, null);
		}
		public static void ClearAll()
		{
			StorageMgr.snsMsg.clearAll();
			SnsMsgMgr.setSnsMsgUnreadCount(0);
		}
		public static SnsComment toSnsComment(SnsMsg snsMsg_0)
		{
			return new SnsComment
			{
				strUserName = snsMsg_0.strFromUsername,
				strNickName = snsMsg_0.strFromNickname,
				nSource = snsMsg_0.nSource,
				nType = snsMsg_0.nType,
				strContent = snsMsg_0.strContent,
				nCreateTime = snsMsg_0.nCreateTime,
				nCommentId = snsMsg_0.nCommentId,
				nReplyCommentId = snsMsg_0.nReplyCommentId
			};
		}
		public static List<SnsComment> getMsgByUserName(string strUserName, string strObjectID, int start = 0, int int_0 = 2147483647)
		{
			List<SnsMsg> listByUserName = StorageMgr.snsMsg.getListByUserName(strUserName, strObjectID, start, int_0);
			if (listByUserName != null && listByUserName.get_Count() > 0)
			{
				List<SnsComment> list = new List<SnsComment>();
				using (List<SnsMsg>.Enumerator enumerator = listByUserName.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsMsg current = enumerator.get_Current();
						list.Add(SnsMsgMgr.toSnsComment(current));
					}
				}
				return Enumerable.ToList<SnsComment>(Enumerable.OrderBy<SnsComment, uint>(list, (SnsComment snsComment_0) => snsComment_0.nCreateTime));
			}
			return null;
		}
		public static bool deleteMsg(ulong snsObjectID)
		{
			return StorageMgr.snsMsg.deleteMsg(SnsInfo.toStrID(snsObjectID));
		}
	}
}
